using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Get, "PsSpecUserPermission")]
    public class GetPsSpecUserPermissionCommand : Cmdlet
    {
        [Parameter(Mandatory = false, Position = 0, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 50)]
        public String ProjectName
        {
            get
            {
                return _projectName;
            }
            set
            {
                _projectName = value;
            }
        }
        private string _projectName = "*";

        [Parameter(Mandatory = false, Position = 1, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 50)]
        public String User
        {
            get
            {
                return _user;
            }
            set
            {
                _user = value;
            }
        }
        private string _user = "*";

        [Parameter(Mandatory = false, Position = 2, ValueFromPipelineByPropertyName = true)]
        public PsSpecProjectRole Role
        {
            get
            {
                return _role;
            }
            set
            {
                _role = value;
            }
        }
        private PsSpecProjectRole _role = PsSpecProjectRole.Unspecified;

        [Parameter(Mandatory = false, Position = 3)]
        public String ServiceUri
        {
            get
            {
                return _serviceUri;
            }
            set
            {
                _serviceUri = value;
            }
        }
        private string _serviceUri = PsSpecCmdletUtils.Constants.ServiceUri;


        protected override void ProcessRecord()
        {
            if (! PsSpecCmdletUtils.IsValidWildcardedProjectName(ProjectName))
            {
                WriteError(new ErrorRecord(
                    new Exception(resources.InvalidProjectName),
                    "INVALID_PROJECT_NAME",
                    ErrorCategory.InvalidArgument, ProjectName));
                return;
            }

            PsSpecCmdletService.CmdletService service =
                new PsSpecCmdletService.CmdletService();
            service.Url = ServiceUri;
            service.UseDefaultCredentials = true;

            string convertedProjectName = PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(ProjectName);
            string convertedUserName = PsSpecCmdletUtils.ConvertWildcardToSqlWildcard(User);
            string convertedRole = PsSpecCmdletUtils.ConvertEnumToSqlWildcard(Role);

            PsSpecCmdletService.PsSpecUserPermission[] users = 
                service.GetPsSpecUserPermission(convertedProjectName, convertedUserName, convertedRole);

            bool resultFound = false;
            foreach (PsSpecCmdletService.PsSpecUserPermission user in users)
            {
                resultFound = true;
                WriteObject(new PsSpecUserPermission(user.Id, user.Project, user.User, user.Role.ToString()));
            }

            // Generate an error if no items found, and no globbing was done
            if (
                (!resultFound) && 
                ( String.Equals(ProjectName, convertedProjectName, StringComparison.InvariantCultureIgnoreCase) ) && 
                ( String.Equals(User, convertedUserName, StringComparison.InvariantCultureIgnoreCase) ) &&
                (Role != PsSpecProjectRole.Unspecified))

            {
                string message = String.Format(resources.ProjectUserNotFound, User, Role.ToString(), ProjectName);
                WriteError(new ErrorRecord(
                    new Exception(message), 
                    "USER_NOT_FOUND",
                    ErrorCategory.ObjectNotFound,
                    User));
            }
        }
    }
}